package net.tycmc.cty.ctyms.timejob.iems.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tycmc.functions.util.StringUtil;

import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.cty.ctyms.timejob.common.ConfigEnum;
import net.tycmc.cty.ctyms.timejob.common.DateList;
import net.tycmc.cty.ctyms.timejob.common.SMSContentCfg;
import net.tycmc.cty.ctyms.timejob.iems.dao.FaultSMSRepeatSendDAO;
import net.tycmc.cty.ctyms.timejob.iems.dao.SMSInfoDAO;

public class FaultSMSRepeatSendService {
	private FaultSMSRepeatSendDAO faultReSendDAO;
	private SMSInfoDAO smsInfoDAO;

	public FaultSMSRepeatSendDAO getFaultReSendDAO() {
		return faultReSendDAO;
	}

	public void setFaultReSendDAO(FaultSMSRepeatSendDAO faultReSendDAO) {
		this.faultReSendDAO = faultReSendDAO;
	}
	public SMSInfoDAO getSmsInfoDAO() {
		return smsInfoDAO;
	}

	public void setSmsInfoDAO(SMSInfoDAO smsInfoDAO) {
		this.smsInfoDAO = smsInfoDAO;
	}

	/**
	 * 故障12小时报警
	 * @author jiyongtian
	 **/
	public void startFaultRe() {
		List<Map<String,Object>> activeFaults = this.faultReSendDAO.queryActiveFault();	
		
		/*wangkai20141016新增故障提示开关判断，判断逻辑如下
		 * 3.6.6新增
		 * 1，从提示开关表中获取“故障提示开关”为关的设备集合
		   2，对从list进行遍历，如果存在则移除该设备的信息
		*/
		/******************获取故障提示开关为关的设备id的集合begin******************/
		List<String> FltRePromptSwitchDown = this.faultReSendDAO.getFLtRePromtSwitch();
		/******************获取故障提示开关为关的设备id的集合end********************/
		
		// 获取配置的发送时间
		List<String> smsSendTimeList = smsInfoDAO.getSMSCfgSendTime("1");
		List<String> sqlList = new ArrayList<String>();
		List<Object[]> paramList = new ArrayList<Object[]>();
		for(Map<String,Object> oneFault:activeFaults){
			//判断是否存在故障开关为关的设备id如果存在则不处理20141016wangkai
			if(!FltRePromptSwitchDown.contains(MapGetter.getString(oneFault, "MsgP_VclID"))){
				int defTime = getTimeDefRate(oneFault);
				if(defTime>0){
					List<Map<String,Object>> userList = smsInfoDAO.getSMSUsers_Flt(MapGetter.getString(oneFault, "MsgP_FltCode"),MapGetter.getString(oneFault, "DBCode"), MapGetter.getString(oneFault, "MsgP_VclID"),"SMSAS_SMSFlt",true);
					if(null != userList && userList.size()>0 ){
						String time = DateList.getFormatDate(MapGetter.getString(oneFault, "MsgTime"), 11);
						oneFault.put("MsgTime", time);
						String Position = MapGetter.getString(oneFault, "Position");
						String Color = MapGetter.getString(oneFault, "MsgP_SecTypeName");
						if("无".equals(Color) || !StringUtil.isValid(Color)){
//							Color = "其他";
						}else{
							//Color = Color.substring(0,1);
						}
						Position = Position.replaceAll("省", "").replaceAll("市", "").replaceAll("附近", "");
						if(Position.length()>15){
							Position = Position.substring(0, 15);
						}
						if("未知".equals(Position) || !StringUtil.isValid(Position)){
							Position = "暂无位置";
						}
						oneFault.put("Position", Position);
						oneFault.put("MsgP_SecTypeName", Color);
						
						String smsContent = this.getSMSContent(oneFault,defTime);
						for(Map<String,Object> oneUser:userList){
							String SMSAS_SMAFlttw = MapGetter.getString(oneUser, "SMSAS_SMAFlttw");
							//该用户支持故障12小时报警
							if(StringUtil.isValid(SMSAS_SMAFlttw)&&SMSAS_SMAFlttw.equals("true")&&(!checkSMS(smsContent,oneUser))){
								//实时发送短信故障码—红色故障（1：选中）
								String SMSAS_SMSFltIsRed = MapGetter.getString(oneUser, "SMSAS_SMSFltIsRed");
								//故障代码颜色
								String SysISM_CodeColorInfo = MapGetter.getString(oneFault, "MsgP_SecTypeName");
								//类型（1：短信白名单；2：短信过滤；3：实时发送短信）
								String SMSFC_Type = MapGetter.getString(oneUser, "SMSFC_Type");
								
								if(!(SMSAS_SMSFltIsRed.equals("true") && SysISM_CodeColorInfo.equals(ConfigEnum.FltCodeRed.getName())) && SMSFC_Type.equals("2")){
									continue;
								}
								
								//开始时间
								String SMSAS_SMSFltSendBegin = MapGetter.getString(oneUser, "SMSAS_SMSFltSendBegin");
								String SMSAS_SMSFltSendEnd = MapGetter.getString(oneUser, "SMSAS_SMSFltSendEnd");
								
								String SC_SendTime_Bgn = StringUtil.isValid(SMSAS_SMSFltSendBegin) ? SMSAS_SMSFltSendBegin + ":00" : ConfigEnum.SendTime_Bgn.getName();
								String SC_SendTime_End = StringUtil.isValid(SMSAS_SMSFltSendEnd) ? SMSAS_SMSFltSendEnd + ":00" : ConfigEnum.SendTime_End.getName();
								
								//短信--故障发送类型（1：统一设置；2：单独设置）
								String SMSAS_SMSFltSendType = MapGetter.getString(oneUser, "SMSAS_SMSFltSendType");
								//判断故障短信发送类型
								if(SMSAS_SMSFltSendType.equals("2")){
									smsSendTimeList.add(0, SC_SendTime_Bgn);
									smsSendTimeList.add(1, SC_SendTime_End);
								}
														
								Map<String, Object> SMSMap = new HashMap<String, Object>();
								SMSMap.put("SMSContent", smsContent);
								SMSMap.put("DBCode", MapGetter.getString(oneFault, "DBCode"));
								SMSMap.put("SysISM_CodeColorInfo", SysISM_CodeColorInfo);
								SMSMap.put("fltLv",  MapGetter.getString(oneFault, "fltLv"));
								SMSMap.put("MsgP_VclID", MapGetter.getString(oneFault, "MsgP_VclID"));
								this.smsInfoDAO.promptSMSInfoFltTwelve(sqlList, paramList, SMSMap, oneUser, smsSendTimeList);
							}else{
								continue;
							}
						}
					}else{
						continue;
					}
				}else{
					continue;
				}
		     }
	    }
		if(sqlList.size()>0&&paramList.size()>0&&(paramList.size()==sqlList.size())){
			 this.faultReSendDAO.tranSQLs(sqlList, paramList);
		}
	}
	/**
	 * 校验故障12小时报警信息是否已发送过短信。
	 **/
	private boolean checkSMS(String smsContent,
			Map<String, Object> oneUser) {
		Map<String,Object> map = faultReSendDAO.checkSMSRe(smsContent,oneUser);
		if(null!=map&&(MapGetter.getInt(map, "count")>0)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 短信内容
	 * 
	 **/
	private String getSMSContent(Map<String, Object> faultMsg,int defTime) {
		String smsContent = SMSContentCfg.SMSCCfgFltTw.replace("X_VclNum", MapGetter.getString(faultMsg, "VehicleNumber")).replace("X_ESNN",MapGetter.getString(faultMsg, "ESNNumber")).replace("X_Date",MapGetter.getString(faultMsg, "MsgTime")).replace("X_Color",MapGetter.getString(faultMsg, "msgp_sectypename"))
		.replace("X_FltCode", MapGetter.getString(faultMsg, "MsgP_FltCode")).replace("X_FltMsgAbbr", MapGetter.getString(faultMsg, "MsgP_FltAbbr")).replace("X_Position", MapGetter.getString(faultMsg, "Position")).replace("X_User", MapGetter.getString(faultMsg, "VehicleContact")).replace("X_Tel", MapGetter.getString(faultMsg, "VehicleChargedMobile")).replaceAll("X_Tw", "已持续"+12*defTime+"H");
		return smsContent;
	}

	/**
	 * 当前时间和故障发生时间 的差值  / 12;
	 * 
	 **/
	private int getTimeDefRate(Map<String, Object> faultMsg) {
		int reInt = 0;
		String faultTime = MapGetter.getString(faultMsg, "MsgTime");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long timeDef = 0;
		try {
			 timeDef = new Date().getTime()-sdf.parse(faultTime).getTime();
		} catch (ParseException e) {
			
		}
		long hoursDef = timeDef/(1000*60*60);
		if((hoursDef>=12)&&(hoursDef%12<1)){
			reInt = (int)(hoursDef/12);
		}else{
			
		}
		return reInt;
	}
}
